package ynu.lcy.elememicroserviceuserauthservice9091.chain;


import org.springframework.stereotype.Component;
import ynu.lcy.elememicroservicecommon.exception.BusinessException;
import ynu.lcy.elememicroservicecommon.utils.ErrorCode;
import ynu.lcy.elememicroserviceuserauthservice9091.dao.IUserDao;
import ynu.lcy.elememicroserviceuserauthservice9091.entity.User;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 查询用户是否匹配
 * */
@Component
public class UserAuthenticationHandler extends AbstractAuthenticationHandler {
    private static IUserDao staticUserDao;

    @Resource
    private IUserDao userDao;

    @PostConstruct
    public void setUserDao(){
        staticUserDao = userDao;
    }

    @Override
    public void handle(AuthenticationContext context) throws BusinessException {
        User user = staticUserDao.findUserByuserPhoneAndPassword(context.getPhoneNumber(), context.getPassword());
        if (user == null) {
            throw new BusinessException(ErrorCode.Authentication_ERROR,"用户名或密码错误");
        }
        context.setUser(user);
        handleNext(context);
    }
}
